package util

import (
	"fmt"
	"gopkg.in/yaml.v3"
	"io/ioutil"
	"log"
	"net/http"
	"strconv"
	"time"
)

type Config struct {
	// 定义你的配置结构体
	// 这里只是一个示例
	Shell_path string `yaml:"shell_path"`
}

func ReadYAML(filePath string) (*Config, error) {
	// 读取文件内容
	content, err := ioutil.ReadFile(filePath)
	if err != nil {
		return nil, err
	}

	// 解析YAML
	config := &Config{}
	err = yaml.Unmarshal(content, config)
	if err != nil {
		return nil, err
	}

	return config, nil
}

func StrToInt(str string) int {
	num, _ := strconv.Atoi(str)
	return num
}

func CheckUrl(url string, timeOut time.Duration) bool {
	client := http.Client{
		Timeout: timeOut,
	}

	resp, err := client.Get(url)
	if err != nil {
		log.Println("CheckUrl: ", err)
		return false
	}
	defer resp.Body.Close()

	if resp.StatusCode == http.StatusOK {
		fmt.Println("Service is working fine.")
		return true
	}
	return false
}
